
import ModuleManager from "../../cfw/module/ModuleManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CCButtonEx extends cc.Button {


    protected _initedButton: boolean = false;

    // // 静音？
    @property
    soundID: number = 1;

    public sprite: cc.Sprite;

    public self: cc.Button;

    start() {
        this.sprite = this.getComponent(cc.Sprite)
        if (!this.sprite) {
            this.sprite = this.addComponent(cc.Sprite)
            this.self = this.getComponent(cc.Button)

            this.self.target = this.sprite.node;
        }

        let widget = this.getComponent(cc.Widget)
        if (!widget) {
            this.addComponent(cc.Widget)
        }
        this.setTouchEnable(true)

        this.setInteractable(true)
    }

    setTouchEnable(f: boolean) {
        if (f) {
            // SelfButton.setup(this)
            this.node.on(cc.Node.EventType.TOUCH_START, this.onButtonTouched, this);
            this.node.on(cc.Node.EventType.TOUCH_END, this.onButtonTouchEnd, this);
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onButtonTouchCancel, this);
        } else {
            // SelfButton.setup(this)
            this.node.off(cc.Node.EventType.TOUCH_START, this.onButtonTouched, this);
            this.node.off(cc.Node.EventType.TOUCH_END, this.onButtonTouchEnd, this);
            this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onButtonTouchCancel, this);
        }
    }

    // start() {


    //     this.onStart();
    // }

    // onStart() {
    //     if (!this._initedButton) SelfButton.setup(this as SelfButton);
    // }

    // /**添加按钮事件 */
    // addClickHandler(node:cc.Node, component:string, func:string, customUserData:string) {
    //     let handler = new cc.Component.EventHandler();
    //     handler.target = node;
    //     handler.component = component;
    //     handler.handler = func;
    //     handler.customEventData = customUserData;

    //     this.clickEvents.push(handler);
    // }

    // // ---- 通用效果
    // // 配置按钮以满足通用需求
    static setup(btn: CCButtonEx) {
        // let spriteFrame = btn.sprite.spriteFrame;
        // if(spriteFrame){
        //     btn.normalSprite = spriteFrame;
        //     btn.pressedSprite = null;
        //     btn.hoverSprite = null;
        //     btn.disabledSprite = null;
        // btn.transition = cc.Button.Transition.SPRITE;
        // }


        btn.enableAutoGrayEffect = true;

        btn.normalColor = cc.Color.WHITE;
        btn.pressedColor = cc.color(200, 200, 200);
        btn.hoverColor = cc.Color.WHITE;
        btn.disabledColor = cc.Color.WHITE;
        // btn.transition = cc.Button.Transition.COLOR;

        btn.interactable = true;
        btn._initedButton = true;
    }

    // 扩展点击
    onButtonTouched() {
        // cc.log('onButtonTouched ')
    }
    // 扩展结束点击
    onButtonTouchEnd() {

        // if (ModuleManager.getAudio().getMusicFlag()) {
        ModuleManager.publicAudio().playSound('audio/click')
        // }



        // cc.log('onButtonTouchEnd ')
    }
    // 扩展取消点击
    onButtonTouchCancel() {

    }

    setInteractable(b: boolean) {
        if (!this._initedButton) CCButtonEx.setup(this);
        if (!b) {
            this.transition = cc.Button.Transition.SPRITE;
            this.interactable = b;
        } else {
            this.transition = cc.Button.Transition.COLOR;
            this.interactable = b;
        }
    }
}
